import { _decorator, Component, Node } from 'cc';
import { ecs } from 'db://oops-framework/libs/ecs/ECS';
import { ModuleUtil } from 'db://oops-framework/module/common/ModuleUtil';
import { LoginViewComp } from './view/LoginViewComp';
import { UIID } from '../common/config/GameUIConfig';
import { LoginNetComp, LoginNetSystem } from './sys/LoginNetSystem';
import { smc } from '../common/SingletonModuleComp';
import { GateModelComp } from './model/GateModel';
import { GameServerConfig } from '../common/config/GameServerConfig';
import { ShareConfig } from '../../tsrpc/models/ShareConfig';
const { ccclass, property } = _decorator;

@ecs.register(`Login`)
export class Login extends ecs.Entity {
    GateModel:GateModelComp = null!;
    protected init() {
        this.addComponents<ecs.Comp>(GateModelComp)
        // this.addComponents<ecs.Comp>(
        //     GateModelComp,
        //     AccountModelComp);

        // // 实始化TSRPC网络模块
        // smc.net = new CommonNet();

        // // 初始化游戏公共资源
        // this.add(InitResComp);

        ModuleUtil.addViewUiAsync(this, LoginViewComp, UIID.Login);

        
        
    }

    /** 获取区服信息 */
    async getGameArea() {
        var ret = await smc.net.hcGate.callApi(`GameArea`, {});
        if (ret.isSucc) {
            this.GateModel.area = ret.res.area;
            return true;
        }
        else {
            return false;
        }
    }

    public login(server:string, username:string, password:string) {
        const comp = this.add(LoginNetComp);
        comp.username = username;
        comp.password = password;
        comp.server = server;

        GameServerConfig.match = `${ShareConfig.https ? "https" : "http"}://${server}/`;
    }
}

export class EcsLoginSystem extends ecs.System {
    constructor() {
        super();

        this.add(new LoginNetSystem());
    }
}

